
package com.insigma.business.qrcode.soldier.service.impl;



import cn.hutool.core.date.DateUtil;
import com.insigma.business.qrcode.common.service.GovOrgService;
import com.insigma.business.qrcode.common.service.SequenceGenerator;
import com.insigma.business.qrcode.common.util.BizCheckUtil;
import com.insigma.business.qrcode.soldier.dao.PlacementInfoDao;
import com.insigma.business.qrcode.soldier.dto.PlacementInfoDO;
import com.insigma.business.qrcode.soldier.dto.SequenceNameConstant;
import com.insigma.business.qrcode.soldier.request.PlacementInfoBSearcher;
import com.insigma.business.qrcode.soldier.service.PlacementInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;


@Service("placementInfoService")
public class PlacementInfoServiceImpl implements PlacementInfoService {

    @Autowired(required = false)
    private PlacementInfoDao placementInfoDao;
    @Autowired(required = false)
    private SequenceGenerator sequenceGenerator;

    @Autowired
    private GovOrgService govOrgService;
    
    @Override
    public List<PlacementInfoDO> search(final PlacementInfoBSearcher placementInfoBSearcher) {
        return placementInfoDao.search(placementInfoBSearcher);
    }
   
    @Override
    public PlacementInfoDO get(final String id){
        return placementInfoDao.get(id);
    }

    
    @Override
    public Integer insert(final PlacementInfoDO placementInfoDO){
        placementInfoDO.setId(sequenceGenerator.genKey(SequenceNameConstant.PLACEMENT_INFO_ID));
        return placementInfoDao.insert(placementInfoDO);
    }

    
    @Override
    public Integer update(final PlacementInfoDO placementInfoDO){
        return placementInfoDao.update(placementInfoDO);
    }

    
    @Override
    public Integer delete(final String id, final String modifierType, final String modifierId){
        return placementInfoDao.delete(id,modifierType,modifierId);
    }

    @Override
    public PlacementInfoDO getAlivePlacement(String adminAreaCode) {
        String year = DateUtil.format(new Date(), "yyyy");
        return placementInfoDao.getByAdminCodeAndYear(adminAreaCode, year);
    }

    @Override
    public PlacementInfoDO getByAdminCodeAndYear(String adminAreaCode, String year) {
        return placementInfoDao.getByAdminCodeAndYear(adminAreaCode, year);
    }

    @Override
    public void checkPlacement(String employeeId, String placementId) {
        String adminAreaCode = govOrgService.getAdminAreaCodeOfEmploy(employeeId);
        PlacementInfoDO alivePlacement = getAlivePlacement(adminAreaCode);
        BizCheckUtil.checkIsTrue(alivePlacement != null && alivePlacement.getId().equals(placementId));
    }

    @Override
    public PlacementInfoDO getByAdminAreaCode(String adminAreaCode) {
        return placementInfoDao.getByAdminAreaCode(adminAreaCode);
    }

}
